package com.zut.Service.HttpJudgeImpl;

import cn.hutool.json.JSONUtil;
import com.zut.Pojo.Request.CodeEntity;
import com.zut.Pojo.Submissions;
import com.zut.Pojo.language;
import com.zut.Pojo.submissionsBody;
import com.zut.Service.HttpJudge;
import com.zut.Util.HttpClientUtils;
import com.zut.dao.SubmissionsDao;
import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.List;

//@Component
public class HttpJudgeImpl implements HttpJudge {


    private final static String URL="http://192.168.88.140:2358/submissions?base64_encoded=true&wait=false";

//    @Autowired
    private SubmissionsDao submissionsDao;


    /**
     * 根据提供的代码进行普通判断，即无输入值
     * @param body 请求体
     * @return 判断结果
     * TODO 多线程实现
     */
    @SneakyThrows
    @Override
    public Boolean GoJudge(String body) {


        // 将请求体转换为CodeEntity对象
        CodeEntity codeEntity = JSONUtil.toBean(body, CodeEntity.class);

        //获取题目

        // 构建submissionsBody对象
        submissionsBody build=new submissionsBody();
        build.setSource_code(codeEntity.getSourceCode());
        build.setLanguage_id(language.valueOf(codeEntity.getLanguage()).getId());
        build.setStdin(codeEntity.getStdin());
//        submissionsBody build = submissionsBody.builder()
//                // 源代码
//                .source_code(codeEntity.getSourceCode())
//                // 编程语言
//                .language_id(language.valueOf(codeEntity.getLanguage()).getId())
//                // 输入
//                .stdin(codeEntity.getStdin())
//                .build();
        System.err.println(build);


        // 发送POST请求，获取判断结果
        String subToken = HttpClientUtils.post(URL, JSONUtil.toJsonStr(build));

        // 如果判断结果不为空
        if (subToken != null) {
            //TODO 多样例判题返回多个token
            List<String> list=new ArrayList<>();
            list.add(subToken);



            // 构建Submissions对象，更新判断结果
            Submissions update = Submissions.builder()
                    //唯一标识
                    .id(codeEntity.getSubmissionID())
                    .judgmentResult(JSONUtil.toJsonStr(list))
                    .build();

            // 更新数据库中的判断结果
            submissionsDao.updateById(update);
            return true;
        }
        return null;
    }


    

}
